1326B - Maximums - CodeForces Solution


implementation math *900

Please click on ads to support us..

Python Code:

input()
m=0
for x in map(int,input().split()):print(x+m);m+=max(0,x)

C++ Code:

 #include <bits/stdc++.h>
using namespace std;
// typedef
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef pair<long, long> pll;
typedef vector<pii> vpii;
typedef deque<int> di;
typedef deque<ll> dll;
// define instruction
#define double long double
#define rep(i, x, y) for (int i = x; i < y; i++)
#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define all(x) begin(x), end(x)
// define constants
#define MOD 1000000007
#define inf 1e18
#define PI 3.141592653589793238462
// defined functions
ll setBitNumber(ll n)
{
	if (n == 0)
		return 0;
	ll msb = 0;
	n = n / 2;
	while (n != 0)
	{
		n = n / 2;
		msb++;
	}
	return (1 << msb);
}
ll countBits(ll number)
{ // log function in base  take only integer part
	return (ll)log2(number) + 1;
}
ll mul(ll a, ll b, ll mod = 1000000007)
{
	return ((a % mod) * (b % mod)) % mod;
}
ll gcd(ll a, ll b)
{
	if (b == 0)
	{
		return a;
	}
	return gcd(b, a % b);
}
bool isPrime(ll n)
{
	if (n <= 1)
		return false;
	if (n == 2 || n == 3)
		return true;
	if (n % 2 == 0 || n % 3 == 0)
		return false;

	for (ll i = 5; i <= sqrt(n); i = i + 6)
		if (n % i == 0 || n % (i + 2) == 0)
			return false;
	return true;
}
ll expo(ll a, ll b, ll mod)
{
	ll res = 1;
	while (b > 0)
	{
		if (b & 1)
			res = (res * a) % mod;
		a = (a * a) % mod;
		b = b >> 1;
	}
	return res;
}
int countDigit(ll n)
{
	return floor(log10(n) + 1);
}
ll lcm(ll a, ll b)
{
	return (a / gcd(a, b)) * b;
}
ll countDivisors(ll n)
{
	ll cnt = 0;
	for (ll i = 1; i <= sqrt(n); i++)
	{
		if (n % i == 0 && isPrime(i))
		{
			if (n / i == i)
				cnt++;
			else // Otherwise count both
				cnt = cnt + 2;
		}
	}
	return cnt;
}

int fact(int n);

int nCr(int n, int r)
{
	return fact(n) / (fact(r) * fact(n - r));
}

// Returns factorial of n
int fact(int n)
{
	if (n == 0)
		return 1;
	int res = 1;
	for (int i = 2; i <= n; i++)
		res = res * i;
	return res;
}
bool binarySearch(vector<ll> v, ll To_Find)
{
	ll lo = 0, hi = v.size() - 2;
	ll mid;
	// This below check covers all cases , so need to check
	// for mid=lo-(hi-lo)/2
	while (hi - lo > 1)
	{
		int mid = (hi + lo) / 2;
		if (v[mid] < To_Find)
		{
			lo = mid + 1;
		}
		else
		{
			hi = mid;
		}
	}
	if (v[lo] == To_Find)
	{
		return true;
	}
	else if (v[hi] == To_Find)
	{
		return true;
	}
	else
	{
		return false;
	}
}
unsigned long long power(unsigned long long x, unsigned long long y, unsigned long long p)
{
	unsigned long long res = 1; // Initialize result

	x = x % p; // Update x if it is more than or
	// equal to p

	while (y > 0)
	{

		// If y is odd, multiply x with result
		if (y & 1)
			res = (res * x) % p;

		// y must be even now
		y = y >> 1; // y = y/2
		x = (x * x) % p;
	}
	return res;
}

// Returns n^(-1) mod p
unsigned long long modInverse(unsigned long long n, unsigned long long p)
{
	return power(n, p - 2, p);
}

// Returns nCr % p using Fermat's little
// theorem.
unsigned long long nCrModPFermat(unsigned long long n, unsigned long long r, unsigned long long p)
{
	// If n<r, then nCr should return 0
	if (n < r)
		return 0;
	// Base case
	if (r == 0)
		return 1;

	// Fill factorial array so that we
	// can find all factorial of r, n
	// and n-r
	unsigned long long fac[n + 1];
	fac[0] = 1;
	for (unsigned long long i = 1; i <= n; i++)
		fac[i] = (fac[i - 1] * i) % p;

	return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p;
}
// main solution
//@aniketrajput25
void solve()
{
	int n;
	cin >> n;
	vi arr(n);
	for (int i = 0; i < n; i++)
	{
		cin >> arr[i];
	}
	int maxi = INT_MIN;
	vi ans;
	ans.pb(arr[0]);
	maxi = max(maxi, arr[0]);
	for (int i = 1; i < n; i++)
	{
		ans.pb(maxi + arr[i]);
		maxi = max(maxi, arr[i]+maxi);
	}
	for (auto x : ans)
		cout << x << " ";
	return;
}
int main()
{
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int t = 1;
	// cin >> t;
	while (t--)
	{
		solve();
	}

	return 0;
}
	  	  		   		 		  	      	 	 		


Comments

Submit
0 Comments
More Questions

892B - Wrath
999A - Mishka and Contest
727C - Guess the Array
1625C - Road Optimization
1715D - 2+ doors
267A - Subtractions
1582A - Luntik and Concerts
560A - Currency System in Geraldion
946A - Partition
1068B - LCM
1692E - Binary Deque
679A - Bear and Prime 100
488A - Giga Tower
14A - Letter
1150A - Stock Arbitraging
1552A - Subsequence Permutation
1131F - Asya And Kittens
1475F - Unusual Matrix
133B - Unary
1547A - Shortest Path with Obstacle
624A - Save Luke
1238A - Prime Subtraction
1107C - Brutality
1391B - Fix You
988B - Substrings Sort
312A - Whose sentence is it
513A - Game
1711E - XOR Triangle
688A - Opponents
20C - Dijkstra